<template>
  <el-dialog
    ref="dialog"
    style=""
    :title="text + '人员'"
    :visible.sync="dialogVisible"
    width="30%"
    @close="closeFn"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="人员名称" prop="userName">
        <el-input v-model="form.userName" style="width: 90%" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.roleName" placeholder="请选择" style="width: 90%">
          <el-option label="运营员" value="运营员" />
          <el-option label="维修员" value="维修员" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="form.mobile" style="width: 90%" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="负责区域" prop="regionName">
        <el-select v-model="form.regionName" placeholder="请选择" style="width: 90%">
          <el-option
            v-for="item in region"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="头像" prop="image">
        <UploadImage v-model="form.image" />
      </el-form-item>
      <el-form-item label="状态">
        <el-checkbox v-model="form.status">是否启用</el-checkbox>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button class="btnWhite" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" class="orange" @click="submit">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import UploadImage from './UploadImage.vue'
import { addPeople, getPeopleDetail, getRegion, getRoleList, updatePeople } from '@/api/peoplemanger'
export default {
  name: 'Dialog',
  components: {
    UploadImage
  },
  props: {
    text: {
      type: String,
      default: '新增'
    },
    currentId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      value: 0,
      dialogVisible: false,
      form: {
        // id: '',
        userName: '',
        role: [],
        mobile: '',
        roleId: '',
        regionId: '',
        roleName: '',
        regionName: '',
        status: false,
        image: ''
      },
      region: [],
      roleList: [],
      rules: {
        userName: [{ required: true, message: '请输入', trigger: 'blur' }],
        role: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        regionName: [{ required: true, message: '请输入', trigger: 'blur' }],
        image: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  async created() {
    this.roleList = await getRoleList()
  },
  methods: {
    // 提交表单
    async submit() {
      this.form.role = this.roleList[this.value]
      console.log(this.form.role)
      this.form.regionId = this.region.find(item => item.name === this.form.regionName)?.id
      if (this.text === '编辑') {
        if (this.form.roleName === '运营员') {
          this.form.roleId = 2
        } else {
          this.form.roleId = 3
        }
        await updatePeople({ ...this.form, id: this.currentId })
        this.$message.success('修改成功')
        this.$parent.getListFn()
      } else {
        await addPeople(this.form)
        this.$message.success('添加成功')
        this.$parent.getListFn()
      }
      this.$refs.form.resetFields()
      this.closeFn()
    },
    // 获取区域列表
    async getRegionList() {
      const { currentPageRecords } = await getRegion()
      console.log(currentPageRecords)
      this.region = currentPageRecords
    },
    // 获取人员详情
    async getDetail() {
      const data = await getPeopleDetail(this.currentId)
      console.log(data)
      this.form = { ...data }
    },
    // 打开弹窗
    showDialog() {
      this.getRegionList()
      this.dialogVisible = true
    },
    // 关闭弹窗
    closeFn() {
      this.form = {
        userName: '',
        roleId: '',
        mobile: '',
        regionId: '',
        roleName: '',
        regionName: '',
        status: false,
        image: ''
      }
      this.dialogVisible = false
      this.$refs.form.resetFields()
    }
  }
}

</script>

<style scoped lang='scss'>
.el-upload__tip {
    font-size: 14px;
    color: #bac0cd;
}
::v-deep.el-dialog__body  {
    padding-bottom: 0 !important;
}
.footer {
    text-align: center !important;
}
::v-deep.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 84px;
    height: 84px;
    line-height: 84px;
    text-align: center;
  }
  .avatar {
    width: 84px;
    height: 84px;
    display: block;
  }
  .orange {
    background: linear-gradient(135deg,#ff9743,#ff5e20)!important;
    border: 0;
    &:hover {
        background: linear-gradient(135deg,#ff7d43,#db3602)!important;
    }
  }
  .btnWhite {
      background-color: #fbf4f0!important;
      border: 0;
      color: #655b56!important;
    &:hover {
       background-color: #f3e7e1!important;
    }
  }
</style>
